<template>
  <div>
    <el-card >
     <el-form :inline="true"  :model="formInline" ref="formInline" class="demo-form-inline">
  <el-form-item label="学科名称">
    <!-- <el-input v-model="formInline.user" ></el-input> -->
    <el-input v-model="searchCount"></el-input>
  </el-form-item>

  <el-form-item>
   <el-button @click="clear">清除</el-button>
  <el-button type="primary" @click="Search">搜索</el-button>
  </el-form-item>
  <el-form-item class="addSubject">
     <el-button type="success" icon="el-icon-edit"    @click="openDialog('open')">新增学科</el-button>
  </el-form-item>
</el-form>
  <!-- <div>
    <el-alert
    title="消息提示的文案"
    type="info"
    center
    show-icon>
  </el-alert>
  </div> -->
  <!-- 数据显示条 -->
  <el-alert
    type="info"
    show-icon>
    <template slot="title">
      {{`数据共${titleData}条`}}
    </template>
  </el-alert>
  <!-- dialog弹框 -->
<el-dialog :title="title" :visible.sync="dialogFormVisible" width="450px" @close="closedialog">
  <el-form :model="form" :rules="rules" ref="form">
    <el-form-item label="学科类型" label-width="100px"  prop="subjectName">
      <el-input v-model="form.subjectName" style="width:220px" placeholder="请输入学科名称" ></el-input>
    </el-form-item>
    <el-form-item label="是否显示" label-width="100px">
      <!-- switch开关 -->
      <el-tooltip :content="'Switch value: ' + value" placement="top">
 <el-switch
        :validate-event="false"
        v-model="value"
        active-color="#13ce66"
        inactive-color="#ff4949">
      </el-switch>
</el-tooltip>
    </el-form-item>
  </el-form>
  <template></template>
  <div slot="footer" class="dialog-footer">
    <el-button @click="closeDialog">取 消</el-button>
    <el-button type="primary"  @click="addSubjectData">确 定</el-button>
  </div>
</el-dialog>

  <!-- table -->
  <div>
    <el-table :data="tableData"  style="width: 100%">
      <el-table-column type="index" prop="" label="序号" width="50"></el-table-column>
      <el-table-column prop="subjectName" label="学科名称" width="150"></el-table-column>
      <el-table-column prop="username" label="创建者"  width="120"></el-table-column>
      <el-table-column prop="addDate" label="创建日期" width="220">
        <template #default="date">
          {{date.row.addDate|parseTimeByString}}
        </template>
      </el-table-column>
      <el-table-column prop="isFrontDisplay" label="前台是否显示"  width="120">
        <template #default="obj">{{obj.row.isFrontDisplay === 1 ? '是' : '否'}}</template>
      </el-table-column>
      <el-table-column prop="twoLevelDirectory" label="二级目录"  width="120"></el-table-column>
      <el-table-column prop="tags" label="标签"  width="120"></el-table-column>
      <el-table-column prop="totals" label="题目数量"  width="120"></el-table-column>
      <el-table-column prop="" label="操作" min-width="200">
        <template #default="{row}">
          <el-link @click="todiretory(row)" type="primary" style="margin-right:10px" :underline="false">学科分类</el-link>
          <el-link @click="tag(row)" type="primary" style="margin-right:10px" :underline="false">学科标签</el-link>
          <el-link type="primary" style="margin-right:10px" :underline="false" @click="openDialog('change',row)">修改</el-link>
          <el-link type="primary" style="margin-right:10px" :underline="false" @click="removeSubject(row)">删除</el-link>
        <!-- <el-button type="text"  size="small" @click="openDialog('change',row)">修改</el-button> -->
        <!-- <el-button type="text"  size="small" @click="openDialog('change',row)">修改</el-button> -->
        <!-- <el-button type="text" size="small" @click="removeSubject(row)">删除</el-button> -->
      </template>
      </el-table-column>
    </el-table>
  </div>
  <!-- 分页组件 -->
   <div class="block">
    <!-- <el-pagination
    style="padding:20px"
    background
     @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="page"
      :page-sizes="[5, 10, 15, 20]"
      :page-size="pagesize"
      layout=" prev, pager, next,sizes, jumper"
      :total="total">
    </el-pagination> -->
       <el-pagination
       background
       style="padding:15px 0 20px 0"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="page"
      :page-sizes="[5, 10, 15, 20]"
      :page-size="pagesize"
      layout=" prev, pager, next,sizes, jumper"
      :total="total">
    </el-pagination>
  </div>
    </el-card>
  </div>
</template>

<script>
import { list, add, remove, detail, update } from '@/api/hmmm/subjects'
export default {
  data () {
    return {
      idd: {},
      id: '',
      // 输入框绑定的搜索
      isFrontDisplay: false,
      searchCount: '',
      // switch
      value: true,
      // dialog 弹框
      dialogType: '',
      // dialogTableVisible: false,
      dialogFormVisible: false,
      form: {
        subjectName: ''
      },
      // 顶部显示多少条数据
      titleData: '',
      formInline: {
        contet: ''
      },
      // 当前页
      page: 1,
      // 每页数量
      pagesize: 10,
      // 总条数
      total: 10,
      tableData: [],

      // 新增学科名
      // subjectName: '',
      // 输入框校验
      rules: {
        subjectName: [
          {
            required: true, message: '输入内容不能为空', trigger: 'blur'
          }
        ]
      }

    }
  },
  created () {
    this.getList()
  },
  computed: {
    title () {
      let title = '新增学科'
      if (this.dialogType === 'open') title = '新增学科'
      if (this.dialogType === 'change') title = '修改学科'
      return title
    }
  },
  methods: {
    // 新增学科数据
    async addSubjectData () {
      await this.$refs.form.validate()
      if (this.dialogType === 'open') {
        // 1. 校验表单
        // 2. 发送请求
        // await update(this.form)
        const res = await add({
          subjectName: this.form.subjectName,
          // 新增的是否在当前页显示
          isFrontDisplay: this.value ? 1 : 0
        })
        console.log(res)
        this.$message.success('添加完成')
      }
      if (this.dialogType === 'change') {
        const res = await update({
          isFrontDisplay: this.value ? 1 : 0,
          subjectName: this.form.subjectName,
          id: this.id
        })
        console.log(res)
        this.$message.success('修改成功')
      }
      this.getList()
      this.closedialog()
      // this.$message.success('添加完成')
    },
    closedialog () {
      // 关闭弹窗
      this.dialogFormVisible = false
      // 清空表单
      this.form = {}
      if (this.dialogType !== 'preview') {
        this.$refs.form.resetFields()
      }
    },
    async openDialog (type, data) {
      // this.idd = id
      // const res = await update(type[1].id)
      // if (type === 'change') {
      //   const res = await update(id)
      //   console.log(res)
      // }
      console.log(type)
      this.dialogFormVisible = true
      this.dialogType = type
      // console.log(data.id)
      if (type !== 'open') {
        const res = await detail(data)
        console.log(res)
        this.idd = {
          ...res.data
        }
        this.id = data.id
        this.form = { ...res.data }
        this.value = res.data.isFrontDisplay === 1
        console.log(this.form)
        // console.log(this.idd)/
      }
      if (type === 'open') {
        this.value = true
        this.form = {}
      }
      // console.log(this.idd)
      // this.getList()
    },
    // async openDialog (type, id) {
    //   const res = await update(id)
    //   console.log(res)
    //   this.dialogFormVisible = true
    // },
    // 数据
    async  getList () {
      // console.log(111)
      const res = await list({
        page: this.page,
        pagesize: this.pagesize,
        subjectName: this.searchCount
      })
      console.log(res.data)
      this.titleData = res.data.counts.toString()
      this.tableData = res.data.items
      this.total = res.data.counts
    },

    // dialog弹框 取消按钮
    closeDialog () {
      this.$refs.form.resetFields()
      this.dialogFormVisible = false
    },
    // 切换页数
    handleSizeChange (val) {
      console.log(val)
      this.pagesize = val
      // this.page = 1
      this.getList()
    },
    // 点击页数跳转
    handleCurrentChange (val) {
      console.log(val)
      this.page = val
      this.getList()
    },
    // 删除学科
    async  removeSubject (id) {
      this.$confirm('此操作将永久删除该学科, 是否继续?', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        const res = await remove(id)
        console.log(res)
        this.$message({ type: 'success', message: '删除成功!' })
        this.getList()
      }).catch(() => {})
      // const res = await remove(id)
      this.getList()
      // console.log(res)
    },
    // 去目录
    todiretory (row) {
      const { id, subjectName } = row
      console.log(row)
      this.$router.push({ path: 'directorys', query: { id, subjectName } })
    },
    tag (row) {
      const { id, subjectName } = row
      this.$router.push({ path: 'tags', query: { id, subjectName } })
    },
    Search () {
      this.page = 1
      this.getList()
    },
    clear () {
      this.searchCount = ''
      this.getList()

      // this.
      // this.formInline.resetFields()
      // this.$refs.formInline.resetFields()
    }
  }
}
</script>

<style lang="scss" scoped>
.addSubject{
  position: absolute;
  right: 50px;
}
 .el-table .header-row {
    background: #fafafa;
  }
.el-pagination {
  margin-top: 20px;
  // text-align: right;
  float: right;
}
</style>
